package com.zhongkai.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mysql.jdbc.StringUtils;
import com.zhongkai.Bean.PageBean;
import com.zhongkai.Bean.WebSocketIndBean;
import com.zhongkai.MessageTool;
import com.zhongkai.app.service.IBaseDao;
import com.zhongkai.app.service.IndentAuditService;
import com.zhongkai.app.service.PageQuerDao;
import com.zhongkai.listener.RocketMqProducer;
import com.zhongkai.model.utils.ZhongkaiResult;
import com.zhongkai.model3.Indent;
import com.zhongkai.model3.Indent2;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 采购审核
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class IndentAuditServiceImpl implements IndentAuditService {

    @Autowired
    private IBaseDao iBaseDao;

    @Autowired
    private PageQuerDao pageQuerDao;


    @Autowired
    private RocketMqProducer rocketMqProducer;
/*    @Autowired
    private JmsTemplate jmsTemplate;

    @Resource
    private Destination topicDestination;*/

    /**
     * 审批
     *
     * @param id         商品订单id
     * @param int_ratify 审核人员姓名
     * @param audit_uuid 审核人员你唯一标识
     */

    @Override
    public ZhongkaiResult auditIndent_ok(Long id, String int_ratify, String audit_uuid) {
        if (StringUtils.isNullOrEmpty(int_ratify) || StringUtils.isNullOrEmpty(audit_uuid)
                || id == 0
                ) {
            return ZhongkaiResult.build(400, "数据不完整");
        }
        final Indent indent = (Indent) iBaseDao.get("from Indent ind where ind.id='" + id + "'");

        if (indent == null) {
            return ZhongkaiResult.build(400, "此订单不存在");
        }
        indent.setIntRatify(int_ratify);
        indent.setAuditUuid(audit_uuid); //审核人员uuid

        long millis = new DateTime().getMillis(); //获取当前系统时间戳

        indent.setRatifyTime(millis); //审核时间
        indent.setIndState(2); //1待审批  2已审批 3 驳回
        try {
            iBaseDao.update(indent);

            String dateTime = new DateTime(indent.getIndTime()).toString("yyyy-MM-dd HH:mm:ss");

            WebSocketIndBean indBean = new WebSocketIndBean();
            indBean.setUser_uuid(indent.getPersonUuid());
            indBean.setType("2");
            indBean.setContent("您于 " + dateTime + " 提交的" + indent.getIndName() + "采购申请已通过,当前状态 未入库");

            ObjectMapper mapper = new ObjectMapper();

            String json = null;
            try {
                json = MessageTool.FWEB_DATE + mapper.writeValueAsString(indBean);
                byte[] bytes = json.getBytes("utf-8");
                rocketMqProducer.sendMsgIntime(MessageTool.BIG_SKY, MessageTool.FWEB_DATE, bytes);

            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }


/*            jmsTemplate.send(topicDestination, new MessageCreator() {
                @Override
                public Message createMessage(Session session) throws JMSException {


                    TextMessage textMessage = session.createTextMessage(json);
                    return textMessage;
                }
            });*/

            return ZhongkaiResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return ZhongkaiResult.build(400, "审批失败");
        }

    }


    /**
     * 驳回
     *
     * @param id         商品订单id
     * @param int_ratify 审核人员姓名
     * @param audit_uuid 审核人员你唯一标识
     * @param ind_remark 驳回原因
     */
    @Override
    public ZhongkaiResult auditIndent_error(Long id, String int_ratify, String audit_uuid, final String ind_remark) {

        if (StringUtils.isNullOrEmpty(int_ratify) || StringUtils.isNullOrEmpty(audit_uuid)
                || id == 0 || StringUtils.isNullOrEmpty(ind_remark)
                ) {
            return ZhongkaiResult.build(400, "数据不完整");
        }

        final Indent indent = (Indent) iBaseDao.get("from Indent ind where ind.id='" + id + "'");

        if (indent == null) {
            return ZhongkaiResult.build(400, "此订单不存在");
        }


        indent.setIntRatify(int_ratify);
        indent.setAuditUuid(audit_uuid); //审核人员uuid

        long millis = new DateTime().getMillis(); //获取当前系统时间戳

        indent.setRatifyTime(millis); //驳回时间
        indent.setIndState(3); //1待审批  2已审批 3 驳回
        indent.setIndRemark(ind_remark); //驳回原因
        try {
            iBaseDao.update(indent);

            String dateTime = new DateTime(indent.getIndTime()).toString("yyyy-MM-dd HH:mm:ss");

            WebSocketIndBean indBean = new WebSocketIndBean();
            indBean.setUser_uuid(indent.getPersonUuid());
            indBean.setType("1");
            indBean.setContent("您于 " + dateTime + " 提交的" + indent.getIndName() + "采购申请未通过,请修改后重新提交,谢谢~  未通过原因: " + ind_remark);

            ObjectMapper mapper = new ObjectMapper();

            String json = null;
            try {
                json = MessageTool.FWEB_DATE + mapper.writeValueAsString(indBean);
                byte[] bytes = json.getBytes("utf-8");
                rocketMqProducer.sendMsgIntime(MessageTool.BIG_SKY, MessageTool.FWEB_DATE, bytes);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }


/*            jmsTemplate.send(topicDestination, new MessageCreator() {
                @Override
                public Message createMessage(Session session) throws JMSException {


                    TextMessage textMessage = session.createTextMessage(json);
                    return textMessage;
                }
            });*/
            return ZhongkaiResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return ZhongkaiResult.build(400, "驳回失败");
        }
    }

    @Override
    public ZhongkaiResult auditIndent_list(Integer page, Integer rows) {
        if (page == 0 || rows == 0) {
            return ZhongkaiResult.build(400, "数据不完整");
        }


        try {
            PageBean pageBean = new PageBean();
            pageBean.setCurrentPage(page);
            pageBean.setPageSize(rows);
            DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Indent.class);
            detachedCriteria.add(Restrictions.eq("indRemove", "1")); //增加条件
            detachedCriteria.addOrder(Order.desc("indTime")); //根据传入的字段进行降序排序
            pageBean.setDetachedCriteria(detachedCriteria);
            pageQuerDao.pageQuery(pageBean);
            return ZhongkaiResult.ok(pageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return ZhongkaiResult.build(405, "查询失败");
        }
    }


    //..........................................办公采购.................................

    /**
     * 审批
     *
     * @param id         商品订单id
     * @param int_ratify 审核人员姓名
     * @param audit_uuid 审核人员你唯一标识
     */

    @Override
    public ZhongkaiResult auditIndent_ok2(Long id, String int_ratify, String audit_uuid) {
        if (StringUtils.isNullOrEmpty(int_ratify) || StringUtils.isNullOrEmpty(audit_uuid)
                || id == 0
                ) {
            return ZhongkaiResult.build(400, "数据不完整");
        }
        final Indent2 indent = (Indent2) iBaseDao.get("from Indent2 ind where ind.id='" + id + "'");

        if (indent == null) {
            return ZhongkaiResult.build(400, "此订单不存在");
        }
        indent.setIntRatify(int_ratify);
        indent.setAuditUuid(audit_uuid); //审核人员uuid

        long millis = new DateTime().getMillis(); //获取当前系统时间戳

        indent.setRatifyTime(millis); //审核时间
        indent.setIndState(2); //1待审批  2已审批 3 驳回
        try {
            iBaseDao.update(indent);
            String dateTime = new DateTime(indent.getIndTime()).toString("yyyy-MM-dd HH:mm:ss");

            WebSocketIndBean indBean = new WebSocketIndBean();
            indBean.setUser_uuid(indent.getPersonUuid());
            indBean.setType("2");
            indBean.setContent("您于 " + dateTime + " 提交的" + indent.getIndName() + "采购申请已通过,当前状态 未入库");

            ObjectMapper mapper = new ObjectMapper();

            String json = null;

            json = MessageTool.FWEB_DATE + mapper.writeValueAsString(indBean);
            byte[] bytes = json.getBytes("utf-8");
            rocketMqProducer.sendMsgIntime(MessageTool.BIG_SKY, MessageTool.FWEB_DATE, bytes);


/*            jmsTemplate.send(topicDestination, new MessageCreator() {
                @Override
                public Message createMessage(Session session) throws JMSException {


                    TextMessage textMessage = session.createTextMessage(json);
                    return textMessage;
                }
            });*/
            return ZhongkaiResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return ZhongkaiResult.build(400, "审批失败");
        }

    }


    /**
     * 驳回
     *
     * @param id         商品订单id
     * @param int_ratify 审核人员姓名
     * @param audit_uuid 审核人员你唯一标识
     * @param ind_remark 驳回原因
     */
    @Override
    public ZhongkaiResult auditIndent_error2(Long id, String int_ratify, String audit_uuid, final String ind_remark) {

        if (StringUtils.isNullOrEmpty(int_ratify) || StringUtils.isNullOrEmpty(audit_uuid)
                || id == 0 || StringUtils.isNullOrEmpty(ind_remark)
                ) {
            return ZhongkaiResult.build(400, "数据不完整");
        }

        final Indent2 indent = (Indent2) iBaseDao.get("from Indent2 ind where ind.id='" + id + "'");

        if (indent == null) {
            return ZhongkaiResult.build(400, "此订单不存在");
        }


        indent.setIntRatify(int_ratify);
        indent.setAuditUuid(audit_uuid); //审核人员uuid

        long millis = new DateTime().getMillis(); //获取当前系统时间戳

        indent.setRatifyTime(millis); //驳回时间
        indent.setIndState(3); //1待审批  2已审批 3 驳回

        indent.setIndRemark(ind_remark); //驳回原因
        try {
            iBaseDao.update(indent);

            String dateTime = new DateTime(indent.getIndTime()).toString("yyyy-MM-dd HH:mm:ss");

            WebSocketIndBean indBean = new WebSocketIndBean();
            indBean.setUser_uuid(indent.getPersonUuid());
            indBean.setType("1");
            indBean.setContent("您于 " + dateTime + " 提交的" + indent.getIndName() + "采购申请未通过,请修改后重新提交,谢谢~  未通过原因: " + ind_remark);

            ObjectMapper mapper = new ObjectMapper();


            String json = MessageTool.FWEB_DATE + mapper.writeValueAsString(indBean);

            byte[] bytes = json.getBytes("utf-8");

            rocketMqProducer.sendMsgIntime(MessageTool.BIG_SKY, MessageTool.FWEB_DATE, bytes);



/*            jmsTemplate.send(topicDestination, new MessageCreator() {
                @Override
                public Message createMessage(Session session) throws JMSException {


                    TextMessage textMessage = session.createTextMessage(json);
                    return textMessage;
                }
            });*/
            return ZhongkaiResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return ZhongkaiResult.build(400, "驳回失败");
        }
    }

    @Override
    public ZhongkaiResult auditIndent_list2(Integer page, Integer rows) {
        if (page == 0 || rows == 0) {
            return ZhongkaiResult.build(400, "数据不完整");
        }


        try {
            PageBean pageBean = new PageBean();
            pageBean.setCurrentPage(page);
            pageBean.setPageSize(rows);
            DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Indent2.class);
            detachedCriteria.add(Restrictions.eq("indRemove", "1")); //增加条件
            detachedCriteria.addOrder(Order.desc("indTime")); //根据传入的字段进行降序排序
            pageBean.setDetachedCriteria(detachedCriteria);
            pageQuerDao.pageQuery(pageBean);
            return ZhongkaiResult.ok(pageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return ZhongkaiResult.build(405, "查询失败");
        }
    }


}
